<script type="text/javascript">
function showConfirmDialogWithLink(option){
    Ext.MessageBox.show({
        title:'Customer Connect',
        msg:option.msg,
        buttons:Ext.Msg.YESNO,
        fn:function (btn) {

            if (btn == 'no') {
                return false;
            } else {
				console.log(option.direct);
                if(option.direct != null) window.location = option.link;
                else   save(option);
				
               
            }
        }
    })
        
}
function showCmNotice(){
	   Ext.MessageBox.show({
        title:'Customer Connect',
        msg:'You are not authorized to use this feature',
         buttons:Ext.Msg.YES,
      
    })
	}
</script>
<?php if ($action == 'update'): ?>
    <?php if ($model->salesordersorg != NULL): ?>
        <li><a class="return-button" href="javascript:void(0)" onclick="save({link:'index.php?r=invoices/update&invoice=<?= $model->salesordersorg ?>'})">Save & Return</a></li>
    <?php endif ?>
    <li><a class="save-button"  href="javascript:void(0)" onclick="save(null);">Save</a></li>
    <li><a class="cancel-button"  href="index.php?r=invoices/cancel&invoice=<?= $model->invoice ?>">Cancel</a></li>
    <li><a class="delete-button"  href="index.php?r=invoices/cancel&delete=1&invoice=<?= $model->invoice ?>"
           onclick="return confirm('Are you sure to delete this item?')">Delete</a></li>
    <br/>
    <?php if ($model->getSalesOrders() != NULL): ?>
        <li><a class="view-button" href="index.php?r=invoices/update&invoice=<?= $model->getSalesOrders()->invoice ?>" onclick="">View Sales Order</a></li>
    <?php endif ?>
    <br/><h5 style="padding-left:11px;"> Delivery</h5>
    <li><a class="print-button" href="javascript:void(0)" onclick="save({link:'index.php?r=pdf&invno=<?= $model->invoice ?>',newtab:true,print:true})">View/Print</a></li>
 <?php if($model->location->disableemails&&!$model->hold): ?>
    <li><a  class="email-button"  href="#" id="mailForm" onclick="">E-Mail</a></li>
    <?php else: ?>
       <li><a  class="email-button"  href="#" id="mailEmpForm" onclick="">E-Mail</a></li>
    <?php endif; ?>

    <br/>
    <?php 
      $user = new Users();
     
   if($user->checkPermission('invoices/createCreditMemo',Yii::app()->user->id)):
    ?>
    <li><a class="add-button" href="javascript:void(0)" 
           onclick="showConfirmDialogWithLink({link:'index.php?r=invoices/createCreditMemo&invoice=<?php echo $model->invoice; ?>',newtab:true,msg:'Are you sure you want to create a credit memo?'})">Create Credit Memo</a></li>
<?php else:?>
  <li><a class="add-button" href="javascript:void(0)" 
           onclick="showCmNotice()">Create Credit Memo</a></li>
<?php endif;?>
<?php elseif ($action == 'view'): ?>

    <?php if ($model->getSalesOrders() != NULL): ?>
        <li><a href="index.php?r=invoices/update&invoice=<?= $model->getSalesOrders()->invoice ?>">Back To Sales Order</a></li>
    <?php endif ?>
    <li><a class="delete-button"  href="index.php?r=invoices/cancel&delete=1&invoice=<?= $model->invoice ?>"
           onclick="return confirm('Are you sure to delete this item?')">Delete</a></li>
    <li><a class="print-button" target="_blank" onclick="printSelect({link:'index.php?r=pdf&invno=<?= $model->invoice ?>',newtab:true})" >View/Print</a>
       <br/><h5 style="padding-left:11px;"> Delivery</h5>
     <?php if($model->location->disableemails&&!$model->hold): ?>
    <li><a  class="email-button"  href="#" id="mailForm" onclick="">E-Mail</a></li>
    <?php else: ?>
       <li><a  class="email-button"  href="#" id="mailEmpForm" onclick="">E-Mail</a></li>
    <?php endif; ?>

    <br/>

   <?php 
      $user = new Users();
     
   if($user->checkPermission('invoices/createCreditMemo',Yii::app()->user->id)):
    ?>
    <li><a class="add-button" href="javascript:void(0)" 
           onclick="showConfirmDialogWithLink({link:'index.php?r=invoices/createCreditMemo&invoice=<?php echo $model->invoice; ?>',newtab:true,direct:true,msg:'Are you sure you want to create a credit memo?'})">Create Credit Memo</a></li>
<?php else:?>
  <li><a class="add-button" href="javascript:void(0)" 
           onclick="showCmNotice()">Create Credit Memo</a></li>
<?php endif;?>
<?php endif; ?>
<?php
if ($model->jobnumber != null) {
    echo '<li>';
    echo CHtml::link('Go to Job', array('/jobs/update', 'jobid' => $model->jobnumber), array('class' => 'return-button'));
    echo '</li>';
}?>
  <br clear ="both"/>
<?php if (!empty($model->creditMemoList)): ?>
    <h5 style="padding-left:11px;"> Credit Memos</h5>
    <?php foreach ($model->creditMemoList as $memo): ?>
      <li>  <?= CHtml::link($memo->invoice, array('update', 'invoice' => $memo->invoice),array('target'=>'_blank')); ?> : 
        $ <?= number_format(-$memo->amountcharge, 2); ?></li>
    
    <?php endforeach; ?>
<?php endif; ?>   
